package com.litchi.fragment;

import com.litchi.R;
import com.lib.uil.ToastUtils;
import com.lib.uil.UILUtils;
import com.litchi.activity.AddressActivity;
import com.litchi.activity.FavorActivity;
import com.litchi.activity.FeedBackActivity;
import com.litchi.activity.HistoryActivity;
import com.litchi.activity.LoginActivity;
import com.litchi.activity.MainActivity;
import com.litchi.activity.MessageCenterActivity;
import com.litchi.activity.MoreActivity;
import com.litchi.activity.OrdersActivity;
import com.litchi.activity.PurseActivity;
import com.litchi.activity.WebActivity;
import com.litchi.utils.Constants;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class MineFragment extends Fragment implements OnClickListener {

	private View layout;
	private View mViewNotLogined;
	private View mViewLogined;
	private TextView mTvUid;
	private ImageView mImgUserIcon;
	private String uid;
	private Context mContext;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (layout != null) {
			initLogin();
			// 防止多次new出片段对象，造成图片错乱问题
			return layout;
		}
		layout = inflater.inflate(R.layout.fragment_mine, container, false);
		mContext=getActivity();
		initView();
		setOnListener();
		initLogin();
		return layout;
	}

	/**
	 * 初始化登录信息
	 * 
	 * @param activity
	 * @param isLogined
	 */
	private void initLogin() {
		MainActivity activity = (MainActivity) getActivity();
		boolean isLogined = activity.getLogined();
		if (isLogined) {
			// 读取登录类型
			 
			uid = activity.getUid();
			mViewNotLogined.setVisibility(View.GONE);
			mViewLogined.setVisibility(View.VISIBLE);
			mTvUid.setText(uid);
		} else {
			mViewNotLogined.setVisibility(View.VISIBLE);
			mViewLogined.setVisibility(View.GONE);
		}
	}

	private void setOnListener() {
		layout.findViewById(R.id.layout_mine_order).setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_wallet).setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_messages).setOnClickListener(this);
		layout.findViewById(R.id.personal_login_button)
				.setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_feedback).setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_collects).setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_history).setOnClickListener(this);
		layout.findViewById(R.id.layout_mine_account_center)
				.setOnClickListener(this);
		layout.findViewById(R.id.tv_more).setOnClickListener(this);
	}

	private void initView() {
		mViewNotLogined = layout.findViewById(R.id.layout_not_logined);
		mViewLogined = layout.findViewById(R.id.layout_logined);
		mTvUid = (TextView) layout.findViewById(R.id.tv_uid);
		mImgUserIcon = (ImageView) layout.findViewById(R.id.user_icon);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		// 将layout从父组件中移除
		ViewGroup parent = (ViewGroup) layout.getParent();
		parent.removeView(layout);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Constants.INTENT_KEY.LOGIN_REQUEST_CODE) {
			if (resultCode == Constants.INTENT_KEY.LOGIN_RESULT_SUCCESS_CODE) {

				uid = data.getStringExtra(Constants.USER_ID_KEY);
				mTvUid.setText(uid);
				MainActivity activity = (MainActivity) getActivity();
				activity.setIsLogined(true, uid);
				mViewNotLogined.setVisibility(View.GONE);
				mViewLogined.setVisibility(View.VISIBLE);
			}

		} else if (requestCode == Constants.INTENT_KEY.REQUEST_MOREACTIVITY) {
			initLogin();
		}
	}

	@Override
	public void onClick(View v) {
		
		SharedPreferences sp = mContext.getSharedPreferences(com.litchi.utils.Constants.USER_LOGIN_SP_KEY, Context.MODE_PRIVATE);
		String userKey = sp.getString(Constants.USER_KEY, null);
		switch (v.getId()) {
		case R.id.personal_login_button: // 登录
			login();
			break;
		case R.id.layout_mine_feedback: // 意见反馈
			 
			startActivity(new Intent(mContext, FeedBackActivity.class));
			break;

		case R.id.tv_more: // 更多
			Intent intent2 = new Intent(getActivity(), MoreActivity.class);
			startActivityForResult(intent2,
					Constants.INTENT_KEY.REQUEST_MOREACTIVITY);
			break;
		case R.id.layout_mine_order: // 全部订单
			if (null!=userKey) {
			startActivity(new Intent(getActivity(), OrdersActivity.class));
			}else {
				ToastUtils.showToast(mContext, "请检查是否登录");
			}
			break;
		case R.id.layout_mine_wallet: // 我的钱包
			if (null!=userKey) {
				Intent walletIntent=new Intent(getActivity(), PurseActivity.class);
				walletIntent.putExtra(Constants.USER_KEY, userKey);
				startActivity(walletIntent);
			}else {
				ToastUtils.showToast(mContext, "请检查是否登录");
			}
			
			break;
		case R.id.layout_mine_messages: // 我的消息
			startActivity(new Intent(getActivity(), MessageCenterActivity.class));
			break;
		case R.id.layout_mine_collects: // 我的关注
			startActivity(new Intent(getActivity(), FavorActivity.class));
			break;
		case R.id.layout_mine_history: // 浏览记录
			startActivity(new Intent(getActivity(), HistoryActivity.class));
			break;

		case R.id.layout_mine_account_center: // 地址信息管理
			
			

			if (sp.contains(com.litchi.utils.Constants.USER_KEY)) {
				
				if (null!=userKey) {
					Intent accountIntent=new Intent(getActivity(), AddressActivity.class);
					accountIntent.putExtra(Constants.USER_KEY, userKey);
					startActivity(accountIntent);

				}
			}else {
				ToastUtils.showToast(mContext, "请检查是否登录");
			}
			
			break;

		default:
			break;
		}
	}

	private void login() {
		Intent intent = new Intent(getActivity(), LoginActivity.class);
		startActivityForResult(intent, Constants.INTENT_KEY.LOGIN_REQUEST_CODE);
	}

}
